// 修改类
var express = require("express");
var dayjs = require("dayjs");
var router = express.Router();
const db = require("../disposition/db");
const path = require("path");
var formidable = require("formidable");
var key = require("../disposition/key");
var fs = require("fs");
// 角色修改————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
router.post("/ModifyTheRole", (req, res) => {
  console.log("角色修改");
  if (req.body.state === "审核库") {
    // 查找审核库是否有对应数据
    const sql = `SELECT * from character_guide WHERE id=${req.body.id} AND Agree=0`;
    console.log(sql);
    db.query(sql, (err, result) => {
      if (err) {
        return;
      }
      if (result.length === 0) {
        res.send({
          msg: "找不到对应数据",
          success: false,
        });
      } else {
        //  判断该数据是否还未通过审批
        if (result[0].Agree === 0) {
          //将时间变化为可赋值字段
          req.body.row.Created_Data = req.body.row.Created_Data.split("T")[0];
          // 获取前端key字段和Value提取出来
          let Key = Object.keys(req.body.row);
          let Value = Object.values(req.body.row);
          // 数据库赋值数据字段的字段
          let Values = [];
          // 循环前端传回来的数据valise
          Key.some((item, index) => {
            // 如果为空值则赋值null
            if (
              (Value[index] === null && item !== "id") ||
              (Value[index] === "" && item !== "id")
            ) {
              Values.push(`${item}=null`);
            } else if (typeof Value[index] === "object" && item !== "id") {
              // 如果是数组则转化完后赋值
              Values.push(`${item}='${JSON.stringify(Value[index])}'`);
            } else if (item !== "id") {
              // 其余照常赋值
              Values.push(`${item}='${Value[index]}'`);
            }
          });

          // 修改审核数据
          var sql2 = `update character_guide set ${Values.join(
            ","
          )} where id = ${req.body.id}`;
          db.query(sql2, function (err, rows) {
            if (err) {
              res.send({
                msg: "修改失败",
                success: false,
              });
            } else {
              res.send({
                msg: "修改成功",
                success: true,
              });
            }
          });
        } else {
          res.send({
            msg: "该审核数据已经被审核",
            success: false,
          });
        }
      }
    });
  } else if (req.body.state === "正式库") {
    // 获取当前时间的拼接
    let DateItem = `${new Date().getFullYear()}-${
      new Date().getMonth() + 1 <= 10
        ? `0${new Date().getMonth() + 1}`
        : new Date().getMonth() + 1
    }-${
      new Date().getDate() <= 10
        ? `0${new Date().getDate()}`
        : new Date().getDate()
    }`;
    // 重新赋值创建时间
    req.body.row.Created_Data = DateItem;
    // 才行赋值传入id、创建人名称
    req.body.row.Created_ID = req.auth.id;
    req.body.row.Created_By = req.auth.name;
    // 重新赋值是否同意、同意则id、同意则名称
    req.body.row.Agree = 0;
    req.body.row.AgreeID = null;
    req.body.row.AgreeName = null;
    // 重新赋值修改库、增还是修字段
    req.body.row.storehouse = 1;
    req.body.row.state = 1;
    // 将日期变化为可赋值值
    req.body.row.Created_Data = req.body.row.Created_Data.split("T")[0];
    // 获取前端key字段并与后端新加字段合并转化成字符串
    let Key = Object.keys(req.body.row);
    let Value = Object.values(req.body.row);
    // 数据库赋值数据字段的字段
    let Values = [];
    // 循环前端传回来的数据valise
    Key.some((item, index) => {
      // 如果为空值则赋值null
      if (Value[index] === null || Value[index] === "") {
        Values.push(`${item}=null`);
      } else if (typeof Value[index] === "object") {
        // 如果是数组则转化完后赋值
        Values.push(`${item}='${JSON.stringify(Value[index])}'`);
      } else {
        // 其余照常赋值
        Values.push(`${item}='${Value[index]}'`);
      }
    });

    // 请求数据库
    const sql2 = `update character_guide set ${Values.join(",")} where id = ${
      req.body.id
    } `;

    db.query(sql2, function (err, rows) {
      if (err) {
        res.send({
          msg: "修改失败！",
          success: false,
        });
      } else {
        res.send({
          msg: "修改成功",
          success: true,
        });
      }
    });
  } else {
    res.send({
      msg: "修改失败",
      success: false,
    });
  }
});
// 灯幻镜修改————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
router.post("/ModifyTheLampMirror", (req, res) => {
  console.log("灯幻镜修改");
  if (req.body.state === "审核库") {
    // 查找审核库是否有对应数据
    const sql = `SELECT * from slide_projector WHERE id=${req.body.id} AND Agree=0`;
    db.query(sql, (err, result) => {
      if (err) {
        return;
      }
      if (result.length === 0) {
        res.send({
          msg: "找不到对应数据",
          success: false,
        });
      } else {
        //  判断该数据是否还未通过审批
        if (result[0].Agree === 0) {
          //将时间变化为可赋值字段
          req.body.row.Created_Data = req.body.row.Created_Data.split("T")[0];
          // 获取前端key字段和Value提取出来
          let Key = Object.keys(req.body.row);
          let Value = Object.values(req.body.row);
          // 数据库赋值数据字段的字段
          let Values = [];
          // 循环前端传回来的数据valise
          Key.some((item, index) => {
            // 如果为空值则赋值null
            if (Value[index] === null || Value[index] === "") {
              Values.push(`${item}=null`);
            } else if (typeof Value[index] === "object") {
              // 如果是数组则转化完后赋值
              Values.push(`${item}='${JSON.stringify(Value[index])}'`);
            } else {
              // 其余照常赋值
              Values.push(`${item}='${Value[index]}'`);
            }
          });

          // 修改审核数据
          var sql2 = `update slide_projector set ${Values.join(
            ","
          )} where id = ${req.body.id}`;
          db.query(sql2, function (err, rows) {
            if (err) {
              res.send({
                msg: "修改失败",
                success: false,
              });
            } else {
              res.send({
                msg: "修改成功",
                success: true,
              });
            }
          });
        } else {
          res.send({
            msg: "该审核数据已经被审核",
            success: false,
          });
        }
      }
    });
  } else if (req.body.state === "正式库") {
    // 查找审核库是否有对应数据
    const sql = `SELECT * from slide_projector WHERE id=${req.body.id} AND Agree=1`;
    db.query(sql, (err, result) => {
      if (err) {
        return;
      }
      if (result.length === 0) {
        res.send({
          msg: "找不到对应数据",
          success: false,
        });
      } else {
        //将时间变化为可赋值字段
        req.body.row.Created_Data = req.body.row.Created_Data.split("T")[0];
        // 将状态，是否同意、创建人、创建人id、创建时间重新赋值
        req.body.row.state = 1;
        req.body.row.Agree = 0;
        req.body.row.Created_By = req.auth.name;
        req.body.row.Created_ID = req.auth.id;
        req.body.row.Created_Data = `${new Date().getFullYear()}-${
          new Date().getMonth() + 1 <= 10
            ? `0${new Date().getMonth() + 1}`
            : new Date().getMonth() + 1
        }-${
          new Date().getDate() <= 10
            ? `0${new Date().getDate()}`
            : new Date().getDate()
        }`;
        // 获取前端key字段和Value提取出来
        let Key = Object.keys(req.body.row);
        let Value = Object.values(req.body.row);
        // 数据库赋值数据字段的字段
        let Values = [];
        // 循环前端传回来的数据valise
        Key.some((item, index) => {
          // 如果为空值则赋值null
          if (Value[index] === null || Value[index] === "") {
            Values.push(`${item}=null`);
          } else if (typeof Value[index] === "object") {
            // 如果是数组则转化完后赋值
            Values.push(`${item}='${JSON.stringify(Value[index])}'`);
          } else {
            // 其余照常赋值
            Values.push(`${item}='${Value[index]}'`);
          }
        });

        // 修改审核数据
        var sql2 = `update slide_projector set ${Values.join(",")} where id = ${
          req.body.id
        }`;
        db.query(sql2, function (err, rows) {
          if (err) {
            res.send({
              msg: "修改失败",
              success: false,
            });
          } else {
            res.send({
              msg: "修改成功",
              success: true,
            });
          }
        });
      }
    });
  } else {
    res.send({
      msg: "修改失败",
      success: false,
    });
  }
});
// 活动修改————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
router.post("/ModifyTheActivity", (req, res) => {
  console.log("活动修改");
  if (req.body.state === "审核库") {
    // 查找审核库是否有对应数据
    const sql = `SELECT * from activity WHERE id=${req.body.id} AND Agree=0`;
    db.query(sql, (err, result) => {
      if (err) {
        return;
      }
      if (result.length === 0) {
        res.send({
          msg: "找不到对应数据",
          success: false,
        });
      } else {
        //  判断该数据是否还未通过审批
        if (result[0].Agree === 0) {
          //将时间变化为可赋值字段
          req.body.row.Created_Data = req.body.row.Created_Data.split("T")[0];
          // 获取前端key字段和Value提取出来
          let Key = Object.keys(req.body.row);
          let Value = Object.values(req.body.row);
          // 数据库赋值数据字段的字段
          let Values = [];
          // 循环前端传回来的数据valise
          Key.some((item, index) => {
            // 如果为空值则赋值null
            if (Value[index] === null || Value[index] === "") {
              Values.push(`${item}=null`);
            } else if (typeof Value[index] === "object") {
              // 如果是数组则转化完后赋值
              Values.push(`${item}='${JSON.stringify(Value[index])}'`);
            } else {
              // 其余照常赋值
              Values.push(`${item}='${Value[index]}'`);
            }
          });

          // 修改审核数据
          var sql2 = `update activity set ${Values.join(",")} where id = ${
            req.body.id
          }`;
          db.query(sql2, function (err, rows) {
            if (err) {
              res.send({
                msg: "修改失败",
                success: false,
              });
            } else {
              res.send({
                msg: "修改成功",
                success: true,
              });
            }
          });
        } else {
          res.send({
            msg: "该审核数据已经被审核",
            success: false,
          });
        }
      }
    });
  } else if (req.body.state === "正式库") {
    // 查找审核库是否有对应数据
    const sql = `SELECT * from activity WHERE id=${req.body.id} AND Agree=1`;
    db.query(sql, (err, result) => {
      if (err) {
        return;
      }
      if (result.length === 0) {
        res.send({
          msg: "找不到对应数据",
          success: false,
        });
      } else {
        //将时间变化为可赋值字段
        req.body.row.Created_Data = req.body.row.Created_Data.split("T")[0];
        // 将状态，是否同意、创建人、创建人id、创建时间重新赋值
        req.body.row.state = 1;
        req.body.row.Agree = 0;
        req.body.row.Created_By = req.auth.name;
        req.body.row.Created_ID = req.auth.id;
        req.body.row.Created_Data = `${new Date().getFullYear()}-${
          new Date().getMonth() + 1 <= 10
            ? `0${new Date().getMonth() + 1}`
            : new Date().getMonth() + 1
        }-${
          new Date().getDate() <= 10
            ? `0${new Date().getDate()}`
            : new Date().getDate()
        }`;
        // 获取前端key字段和Value提取出来
        let Key = Object.keys(req.body.row);
        let Value = Object.values(req.body.row);
        // 数据库赋值数据字段的字段
        let Values = [];
        // 循环前端传回来的数据valise
        Key.some((item, index) => {
          // 如果为空值则赋值null
          if (Value[index] === null || Value[index] === "") {
            Values.push(`${item}=null`);
          } else if (typeof Value[index] === "object") {
            // 如果是数组则转化完后赋值
            Values.push(`${item}='${JSON.stringify(Value[index])}'`);
          } else {
            // 其余照常赋值
            Values.push(`${item}='${Value[index]}'`);
          }
        });

        // 修改审核数据
        var sql2 = `update activity set ${Values.join(",")} where id = ${
          req.body.id
        }`;
        db.query(sql2, function (err, rows) {
          if (err) {
            res.send({
              msg: "修改失败",
              success: false,
            });
          } else {
            res.send({
              msg: "修改成功",
              success: true,
            });
          }
        });
      }
    });
  } else {
    res.send({
      msg: "修改失败",
      success: false,
    });
  }
});
// 道具修改————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
router.post("/ItemModifications", (req, res) => {
  console.log("道具修改");
  if (req.body.state === "审核库") {
    // 查找审核库是否有对应数据
    const sql = `SELECT * from props WHERE id=${req.body.id} AND Agree=0`;
    db.query(sql, (err, result) => {
      if (err) {
        return;
      }
      if (result.length === 0) {
        res.send({
          msg: "找不到对应数据",
          success: false,
        });
      } else {
        //  判断该数据是否还未通过审批
        if (result[0].Agree === 0) {
          //将时间变化为可赋值字段
          req.body.row.Created_Data = dayjs(req.body.row.Created_Data).format(
            "YYYY-MM-DD"
          );
          // 获取前端key字段和Value提取出来
          let Key = Object.keys(req.body.row);
          let Value = Object.values(req.body.row);
          // 数据库赋值数据字段的字段
          let Values = [];
          // 循环前端传回来的数据valise
          Key.some((item, index) => {
            // 如果为空值则赋值null
            if (Value[index] === null || Value[index] === "") {
              Values.push(`${item}=null`);
            } else if (typeof Value[index] === "object") {
              // 如果是数组则转化完后赋值
              Values.push(`${item}='${JSON.stringify(Value[index])}'`);
            } else {
              // 其余照常赋值
              Values.push(`${item}='${Value[index]}'`);
            }
          });

          // 修改审核数据
          var sql2 = `update props set ${Values.join(",")} where id = ${
            req.body.id
          }`;
          db.query(sql2, function (err, rows) {
            if (err) {
              res.send({
                msg: "修改失败",
                success: false,
              });
            } else {
              res.send({
                msg: "修改成功",
                success: true,
              });
            }
          });
        } else {
          res.send({
            msg: "该审核数据已经被审核",
            success: false,
          });
        }
      }
    });
  } else if (req.body.state === "正式库") {
    // 查找审核库是否有对应数据
    const sql = `SELECT * from props WHERE id=${req.body.id} AND Agree=1`;
    db.query(sql, (err, result) => {
      if (err) {
        return;
      }
      if (result.length === 0) {
        res.send({
          msg: "找不到对应数据",
          success: false,
        });
      } else {
        //将时间变化为可赋值字段
        req.body.row.Created_Data = dayjs(req.body.row.Created_Data).format(
          "YYYY-MM-DD"
        );
        // 将状态，是否同意、创建人、创建人id、创建时间重新赋值
        req.body.row.state = 1;
        req.body.row.Agree = 0;
        req.body.row.Created_By = req.auth.name;
        req.body.row.Created_ID = req.auth.id;
        req.body.row.Created_Data = dayjs().format("YYYY-MM-DD");
        // 获取前端key字段和Value提取出来
        let Key = Object.keys(req.body.row);
        let Value = Object.values(req.body.row);
        // 数据库赋值数据字段的字段
        let Values = [];
        // 循环前端传回来的数据valise
        Key.some((item, index) => {
          // 如果为空值则赋值null
          if (Value[index] === null || Value[index] === "") {
            Values.push(`${item}=null`);
          } else if (typeof Value[index] === "object") {
            // 如果是数组则转化完后赋值
            Values.push(`${item}='${JSON.stringify(Value[index])}'`);
          } else {
            // 其余照常赋值
            Values.push(`${item}='${Value[index]}'`);
          }
        });

        // 修改审核数据
        var sql2 = `update props set ${Values.join(",")} where id = ${
          req.body.id
        }`;
        db.query(sql2, function (err, rows) {
          if (err) {
            res.send({
              msg: "修改失败",
              success: false,
            });
          } else {
            res.send({
              msg: "修改成功",
              success: true,
            });
          }
        });
      }
    });
  } else {
    res.send({
      msg: "修改失败",
      success: false,
    });
  }
});
// 调伏战修改————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
router.post("/AmbushWarfare", (req, res) => {
  console.log("调伏战修改");
  if (req.body.state === "审核库") {
    // 查找审核库是否有对应数据
    const sql = `SELECT * from ambush_warfare WHERE id=${req.body.id} AND Agree=0`;
    db.query(sql, (err, result) => {
      if (err) {
        return;
      }
      if (result.length === 0) {
        res.send({
          msg: "找不到对应数据",
          success: false,
        });
      } else {
        //  判断该数据是否还未通过审批
        if (result[0].Agree === 0) {
          //将时间变化为可赋值字段
          req.body.row.Created_Data = req.body.row.Created_Data.split("T")[0];
          // 获取前端key字段和Value提取出来
          let Key = Object.keys(req.body.row);
          let Value = Object.values(req.body.row);
          // 数据库赋值数据字段的字段
          let Values = [];
          // 循环前端传回来的数据valise
          Key.some((item, index) => {
            // 如果为空值则赋值null
            if (Value[index] === null || Value[index] === "") {
              Values.push(`${item}=null`);
            } else if (typeof Value[index] === "object") {
              // 如果是数组则转化完后赋值
              Values.push(`${item}='${JSON.stringify(Value[index])}'`);
            } else {
              // 其余照常赋值
              Values.push(`${item}='${Value[index]}'`);
            }
          });

          // 修改审核数据
          var sql2 = `update ambush_warfare set ${Values.join(
            ","
          )} where id = ${req.body.id}`;
          db.query(sql2, function (err, rows) {
            if (err) {
              res.send({
                msg: "修改失败",
                success: false,
              });
            } else {
              res.send({
                msg: "修改成功",
                success: true,
              });
            }
          });
        } else {
          res.send({
            msg: "该审核数据已经被审核",
            success: false,
          });
        }
      }
    });
  } else if (req.body.state === "正式库") {
    // 查找审核库是否有对应数据
    const sql = `SELECT * from ambush_warfare WHERE id=${req.body.id} AND Agree=1`;
    db.query(sql, (err, result) => {
      if (err) {
        return;
      }
      if (result.length === 0) {
        res.send({
          msg: "找不到对应数据",
          success: false,
        });
      } else {
        //将时间变化为可赋值字段
        req.body.row.Created_Data = req.body.row.Created_Data.split("T")[0];
        // 将状态，是否同意、创建人、创建人id、创建时间重新赋值
        req.body.row.state = 1;
        req.body.row.Agree = 0;
        req.body.row.Created_By = req.auth.name;
        req.body.row.Created_ID = req.auth.id;
        req.body.row.Created_Data = `${new Date().getFullYear()}-${
          new Date().getMonth() + 1 <= 10
            ? `0${new Date().getMonth() + 1}`
            : new Date().getMonth() + 1
        }-${
          new Date().getDate() <= 10
            ? `0${new Date().getDate()}`
            : new Date().getDate()
        }`;
        // 获取前端key字段和Value提取出来
        let Key = Object.keys(req.body.row);
        let Value = Object.values(req.body.row);
        // 数据库赋值数据字段的字段
        let Values = [];
        // 循环前端传回来的数据valise
        Key.some((item, index) => {
          // 如果为空值则赋值null
          if (Value[index] === null || Value[index] === "") {
            Values.push(`${item}=null`);
          } else if (typeof Value[index] === "object") {
            // 如果是数组则转化完后赋值
            Values.push(`${item}='${JSON.stringify(Value[index])}'`);
          } else {
            // 其余照常赋值
            Values.push(`${item}='${Value[index]}'`);
          }
        });

        // 修改审核数据
        var sql2 = `update ambush_warfare set ${Values.join(",")} where id = ${
          req.body.id
        }`;
        db.query(sql2, function (err, rows) {
          if (err) {
            res.send({
              msg: "修改失败",
              success: false,
            });
          } else {
            res.send({
              msg: "修改成功",
              success: true,
            });
          }
        });
      }
    });
  } else {
    res.send({
      msg: "修改失败",
      success: false,
    });
  }
});
// 卡池修改————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
router.post("/CardPoolModification", (req, res) => {
  console.log("卡池修改");
  if (req.body.state === "审核库") {
    // 查找审核库是否有对应数据
    const sql = `SELECT * from card_pools WHERE id=${req.body.id} AND Agree=0`;
    db.query(sql, (err, result) => {
      if (err) {
        return;
      }
      if (result.length === 0) {
        res.send({
          msg: "找不到对应数据",
          success: false,
        });
      } else {
        //  判断该数据是否还未通过审批
        if (result[0].Agree === 0) {
          //将时间变化为可赋值字段
          req.body.row.Created_Data = req.body.row.Created_Data.split("T")[0];
          // 获取前端key字段和Value提取出来
          let Key = Object.keys(req.body.row);
          let Value = Object.values(req.body.row);
          // 数据库赋值数据字段的字段
          let Values = [];
          // 循环前端传回来的数据valise
          Key.some((item, index) => {
            // 如果为空值则赋值null
            if (Value[index] === null || Value[index] === "") {
              Values.push(`${item}=null`);
            } else if (typeof Value[index] === "object") {
              // 如果是数组则转化完后赋值
              Values.push(`${item}='${JSON.stringify(Value[index])}'`);
            } else {
              // 其余照常赋值
              Values.push(`${item}='${Value[index]}'`);
            }
          });

          // 修改审核数据
          var sql2 = `update card_pools set ${Values.join(",")} where id = ${
            req.body.id
          }`;
          db.query(sql2, function (err, rows) {
            if (err) {
              res.send({
                msg: "修改失败",
                success: false,
              });
            } else {
              res.send({
                msg: "修改成功",
                success: true,
              });
            }
          });
        } else {
          res.send({
            msg: "该审核数据已经被审核",
            success: false,
          });
        }
      }
    });
  } else if (req.body.state === "正式库") {
    // 查找审核库是否有对应数据
    const sql = `SELECT * from card_pools WHERE id=${req.body.id} AND Agree=1`;
    db.query(sql, (err, result) => {
      if (err) {
        return;
      }
      if (result.length === 0) {
        res.send({
          msg: "找不到对应数据",
          success: false,
        });
      } else {
        //将时间变化为可赋值字段
        req.body.row.Created_Data = req.body.row.Created_Data.split("T")[0];
        // 将状态，是否同意、创建人、创建人id、创建时间重新赋值
        req.body.row.state = 1;
        req.body.row.Agree = 0;
        req.body.row.Created_By = req.auth.name;
        req.body.row.Created_ID = req.auth.id;
        req.body.row.Created_Data = dayjs().format("YYYY-MM-DD");
        // 获取前端key字段和Value提取出来
        let Key = Object.keys(req.body.row);
        let Value = Object.values(req.body.row);
        // 数据库赋值数据字段的字段
        let Values = [];
        // 循环前端传回来的数据valise
        Key.some((item, index) => {
          // 如果为空值则赋值null
          if (Value[index] === null || Value[index] === "") {
            Values.push(`${item}=null`);
          } else if (typeof Value[index] === "object") {
            // 如果是数组则转化完后赋值
            Values.push(`${item}='${JSON.stringify(Value[index])}'`);
          } else {
            // 其余照常赋值
            Values.push(`${item}='${Value[index]}'`);
          }
        });

        // 修改审核数据
        var sql2 = `update card_pools set ${Values.join(",")} where id = ${
          req.body.id
        }`;
        db.query(sql2, function (err, rows) {
          if (err) {
            res.send({
              msg: "修改失败",
              success: false,
            });
          } else {
            res.send({
              msg: "修改成功",
              success: true,
            });
          }
        });
      }
    });
  } else {
    res.send({
      msg: "修改失败",
      success: false,
    });
  }
});
// 排行榜修改————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
router.post("/RankingModifications", (req, res) => {
  console.log("排行榜修改");
  if (
    req.body.state === "0" ||
    req.body.state === "1" ||
    req.body.state === "2"
  ) {
    //将修改时间、人物、id进行赋值
    req.body.row["AgreeData"] = dayjs().format("YYYY-MM-DD HH:mm:ss");
    req.body.row["AgreeID"] = req.auth.id;
    req.body.row["AgreeName"] = req.auth.name;
    // 获取前端key字段和Value提取出来
    let Key = Object.keys(req.body.row);
    let Value = Object.values(req.body.row);
    // 数据库赋值数据字段的字段
    let Values = [];
    // 循环前端传回来的数据valise
    Key.some((item, index) => {
      // 如果为空值则赋值null
      if (Value[index] === null || Value[index] === "") {
        Values.push(`${item}=null`);
      } else if (typeof Value[index] === "object") {
        // 如果是数组则转化完后赋值
        Values.push(`${item}='${JSON.stringify(Value[index])}'`);
      } else {
        // 其余照常赋值
        Values.push(`${item}='${Value[index]}'`);
      }
    });
    // 修改审核数据
    var sql2 = `update leaderboard set ${Values.join(",")} where id = ${
      req.body.state
    }`;
    db.query(sql2, function (err, rows) {
      if (err) {
        res.send({
          msg: "修改失败",
          success: false,
        });
      } else {
        res.send({
          msg: "修改成功",
          success: true,
        });
      }
    });
  } else {
  }
});
// 模拟卡池充值————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
router.post("/recharge", (req, res) => {
  console.log("模拟卡池充值");
  let leaderboard = null;
  if (req.body.num) {
    const sql = "SELECT * FROM leaderboard WHERE id ='3'";
    // 查抽卡模拟器数据
    db.query(sql, (err, result) => {
      if (err) {
        return;
      }
      result.length === 0
        ? res.send({
            msg: "未知错误",
            success: false,
          })
        : (leaderboard = result[0]);
      // 进行充值操作
      const sql2 = `update leaderboard set num=${
        leaderboard.num + parseInt(req.body.num)
      },text="${req.auth.name}",AgreeData="${dayjs().format(
        "YYYY-MM-DD HH:mm:ss"
      )}" WHERE id=3`;
      console.log(sql2);
      db.query(sql2, function (err, rows) {
        if (err) {
          res.send({
            msg: "充值失败",
            success: false,
          });
        } else {
          res.send({
            msg: "充值成功",
            success: true,
          });
        }
      });
    });
  } else {
    res.send({
      msg: "未知错误",
      success: false,
    });
  }
});
// 修改模拟卡池参数————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
router.post("/parameter", (req, res) => {
  console.log("修改模拟卡池参数");
  let leaderboard = null;
  if (
    req.body.role3 &&
    req.body.LampMirror3 &&
    req.body.role3UP &&
    req.body.LampMirror3UP
  ) {
    const sql = "SELECT * FROM leaderboard WHERE id ='3'";
    // 查抽卡模拟器数据
    db.query(sql, (err, result) => {
      if (err) {
        return;
      }
      result.length === 0
        ? res.send({
            msg: "未知错误",
            success: false,
          })
        : (leaderboard = result[0]);
      // 进行修改参数操作
      const sql2 = `update leaderboard set role3=${
        req.body.role3
      },LampMirror3=${req.body.LampMirror3},role3UP=${
        req.body.role3UP
      },LampMirror3UP=${req.body.LampMirror3UP},text="${
        req.auth.name
      }",AgreeData="${dayjs().format("YYYY-MM-DD HH:mm:ss")}" WHERE id=3`;
      console.log(sql2);
      db.query(sql2, function (err, rows) {
        if (err) {
          res.send({
            msg: "修改失败",
            success: false,
          });
        } else {
          res.send({
            msg: "修改成功",
            success: true,
          });
        }
      });
    });
  } else {
    res.send({
      msg: "未知错误",
      success: false,
    });
  }
});
// 复刻模拟卡池————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
router.post("/Fork", (req, res) => {
  console.log("复刻模拟卡池");
  if (req.body.End && req.body.id) {
    const sql2 = `update simulated_gacha set Start_time="${dayjs().format(
      "YYYY-MM-DD HH:mm:ss"
    )}",End_time="${dayjs(req.body.End).format(
      "YYYY-MM-DD HH:mm:ss"
    )}",AgreeID="${req.auth.id}",AgreeName="${req.auth.name}" WHERE id=${
      req.body.id
    }`;
    console.log(sql2);
    db.query(sql2, function (err, rows) {
      if (err) {
        res.send({
          msg: "设置",
          success: false,
        });
      } else {
        res.send({
          msg: "设置成功",
          success: true,
        });
      }
    });
  } else {
    res.send({
      msg: "未知错误",
      success: false,
    });
  }
});
// 遗迹一层修改————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
router.post("/ModifyTheSubdued", (req, res) => {
  console.log("遗迹一层修改");
  if (req.body.id) {
    // 查找审核库是否有对应数据
    const sql = `SELECT * from enum WHERE id=${req.body.id}`;
    db.query(sql, (err, result) => {
      if (err) {
        return;
      }
      if (result.length === 0) {
        res.send({
          msg: "找不到对应数据",
          success: false,
        });
      } else {
        // 获取前端key字段和Value提取出来
        let Key = Object.keys(req.body.row);
        let Value = Object.values(req.body.row);
        // 数据库赋值数据字段的字段
        let Values = [];
        // 循环前端传回来的数据valise
        Key.some((item, index) => {
          // 如果为空值则赋值null
          if (Value[index] === null || Value[index] === "") {
            Values.push(`${item}=null`);
          } else if (typeof Value[index] === "object") {
            // 如果是数组则转化完后赋值
            Values.push(`${item}='${JSON.stringify(Value[index])}'`);
          } else {
            // 其余照常赋值
            Values.push(`${item}='${Value[index]}'`);
          }
        });
        // 赋值修改人，修改id，修改时间
        Values.push(`Created_By='${req.auth.name}'`);
        Values.push(`Created_ID=${req.auth.id}`);
        Values.push(`Created_Data='${dayjs().format("YYYY-MM-DD")}'`);
        // 修改审核数据
        var sql2 = `update enum set ${Values.join(",")} where id = ${
          req.body.id
        }`;
        console.log(sql2);
        db.query(sql2, function (err, rows) {
          if (err) {
            res.send({
              msg: "修改失败",
              success: false,
            });
          } else {
            res.send({
              msg: "修改成功",
              success: true,
            });
          }
        });
      }
    });
  } else {
    res.send({
      msg: "未知错误",
      success: false,
    });
  }
});
// 遗迹层级修改————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
router.post("/RelicLayerModified", (req, res) => {
  console.log("遗迹层级修改");
  if (req.body.id) {
    // 查找审核库是否有对应数据
    const sql = `SELECT * from relic WHERE id=${req.body.id}`;
    db.query(sql, (err, result) => {
      if (err) {
        return;
      }
      if (result.length === 0) {
        res.send({
          msg: "找不到对应数据",
          success: false,
        });
      } else {
        // 获取前端key字段和Value提取出来
        let Key = Object.keys(req.body.row);
        let Value = Object.values(req.body.row);
        // 数据库赋值数据字段的字段
        let Values = [];
        // 循环前端传回来的数据valise
        Key.some((item, index) => {
          // 如果为空值则赋值null
          if (Value[index] === null || Value[index] === "") {
            Values.push(`${item}=null`);
          } else if (typeof Value[index] === "object") {
            // 如果是数组则转化完后赋值
            Values.push(`${item}='${JSON.stringify(Value[index])}'`);
          } else {
            // 其余照常赋值
            Values.push(`${item}='${Value[index]}'`);
          }
        });
        // 赋值修改人，修改id，修改时间
        Values.push(`Created_By='${req.auth.name}'`);
        Values.push(`Created_ID=${req.auth.id}`);
        Values.push(`Created_Data='${dayjs().format("YYYY-MM-DD")}'`);
        // 修改审核数据
        var sql2 = `update relic set ${Values.join(",")} where id = ${
          req.body.id
        }`;
        db.query(sql2, function (err, rows) {
          if (err) {
            res.send({
              msg: "修改失败",
              success: false,
            });
          } else {
            res.send({
              msg: "修改成功",
              success: true,
            });
          }
        });
      }
    });
  }
});
// 新闻公告修改————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
router.post("/ModifyPressReleases", (req, res) => {
  console.log("新闻公告修改");
  if (req.body.id) {
    // 查找审核库是否有对应数据
    const sql = `SELECT * from press_releases WHERE id=${req.body.id}`;
    db.query(sql, (err, result) => {
      if (err) {
        return;
      }
      if (result.length === 0) {
        res.send({
          msg: "找不到对应数据",
          success: false,
        });
      } else {
        // 获取前端key字段和Value提取出来
        let Key = Object.keys(req.body.row);
        let Value = Object.values(req.body.row);
        // 数据库赋值数据字段的字段
        let Values = [];
        // 循环前端传回来的数据valise
        Key.some((item, index) => {
          // 如果为空值则赋值null
          if (Value[index] === null || Value[index] === "") {
            Values.push(`${item}=null`);
          } else if (typeof Value[index] === "object") {
            // 如果是数组则转化完后赋值
            Values.push(`${item}='${JSON.stringify(Value[index])}'`);
          } else {
            // 其余照常赋值
            Values.push(`${item}='${Value[index]}'`);
          }
        });
        // 赋值修改人，修改id，修改时间
        Values.push(`Created_By='${req.auth.name}'`);
        Values.push(`Created_ID=${req.auth.id}`);
        Values.push(`Created_Data='${dayjs().format("YYYY-MM-DD")}'`);
        // 修改审核数据
        var sql2 = `update press_releases set ${Values.join(",")} where id = ${
          req.body.id
        }`;
        db.query(sql2, function (err, rows) {
          if (err) {
            res.send({
              msg: "修改失败",
              success: false,
            });
          } else {
            res.send({
              msg: "修改成功",
              success: true,
            });
          }
        });
      }
    });
  }
});
// 名词解析修改————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
router.post("/WordAnalysis", (req, res) => {
  console.log("名词解析修改");
  if (req.body.id) {
    // 查找审核库是否有对应数据
    const sql = `SELECT * from word_analysis WHERE id=${req.body.id}`;
    db.query(sql, (err, result) => {
      if (err) {
        return;
      }
      if (result.length === 0) {
        res.send({
          msg: "找不到对应数据",
          success: false,
        });
      } else {
        // 获取前端key字段和Value提取出来
        let Key = Object.keys(req.body.row);
        let Value = Object.values(req.body.row);
        // 数据库赋值数据字段的字段
        let Values = [];
        // 循环前端传回来的数据valise
        Key.some((item, index) => {
          // 如果为空值则赋值null
          if (Value[index] === null || Value[index] === "") {
            Values.push(`${item}=null`);
          } else if (typeof Value[index] === "object") {
            // 如果是数组则转化完后赋值
            Values.push(`${item}='${JSON.stringify(Value[index])}'`);
          } else {
            // 其余照常赋值
            Values.push(`${item}='${Value[index]}'`);
          }
        });
        // 赋值修改人，修改id，修改时间
        Values.push(`Created_By='${req.auth.name}'`);
        Values.push(`Created_ID=${req.auth.id}`);
        Values.push(`Created_Data='${dayjs().format("YYYY-MM-DD")}'`);
        // 修改审核数据
        var sql2 = `update word_analysis set ${Values.join(",")} where id = ${
          req.body.id
        }`;
        db.query(sql2, function (err, rows) {
          if (err) {
            res.send({
              msg: "修改失败",
              success: false,
            });
          } else {
            res.send({
              msg: "修改成功",
              success: true,
            });
          }
        });
      }
    });
  }
});
// 人员管理修改————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
router.post("/User", (req, res) => {
  console.log("名词解析修改");
  if (req.body.id) {
    // 查找审核库是否有对应数据
    const sql = `SELECT * from user WHERE id=${req.body.id}`;
    db.query(sql, (err, result) => {
      if (err) {
        return;
      }
      if (result.length === 0) {
        res.send({
          msg: "找不到对应数据",
          success: false,
        });
      } else {
        // 获取前端key字段和Value提取出来
        let Key = Object.keys(req.body.row);
        let Value = Object.values(req.body.row);
        // 数据库赋值数据字段的字段
        let Values = [];
        // 循环前端传回来的数据valise
        Key.some((item, index) => {
          // 如果为空值则赋值null
          if (Value[index] === null || Value[index] === "") {
            Values.push(`${item}=null`);
          } else if (typeof Value[index] === "object") {
            // 如果是数组则转化完后赋值
            Values.push(`${item}='${JSON.stringify(Value[index])}'`);
          } else {
            // 其余照常赋值
            Values.push(`${item}='${Value[index]}'`);
          }
        });
        // 赋值修改人，修改id，修改时间
        Values.push(`Created_By='${req.auth.name}'`);
        Values.push(`Created_ID=${req.auth.id}`);
        Values.push(`Created_Data='${dayjs().format("YYYY-MM-DD")}'`);
        // 修改审核数据
        var sql2 = `update user set ${Values.join(",")} where id = ${
          req.body.id
        }`;
        db.query(sql2, function (err, rows) {
          if (err) {
            res.send({
              msg: "修改失败",
              success: false,
            });
          } else {
            res.send({
              msg: "修改成功",
              success: true,
            });
          }
        });
      }
    });
  }
});
// 神代盘修改————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
router.post("/GodGeneration", (req, res) => {
  console.log("神代盘修改");
  if (req.body.id) {
    // 查找审核库是否有对应数据
    const sql = `SELECT * from god_generation WHERE id=${req.body.id}`;
    db.query(sql, (err, result) => {
      if (err) {
        return;
      }
      if (result.length === 0) {
        res.send({
          msg: "找不到对应数据",
          success: false,
        });
      } else {
        // 获取前端key字段和Value提取出来
        let Key = Object.keys(req.body.row);
        let Value = Object.values(req.body.row);
        // 数据库赋值数据字段的字段
        let Values = [];
        // 循环前端传回来的数据valise
        Key.some((item, index) => {
          // 如果为空值则赋值null
          if (Value[index] === null || Value[index] === "") {
            Values.push(`${item}=null`);
          } else if (typeof Value[index] === "object") {
            // 如果是数组则转化完后赋值
            Values.push(`${item}='${JSON.stringify(Value[index])}'`);
          } else {
            // 其余照常赋值
            Values.push(`${item}='${Value[index]}'`);
          }
        });
        // 赋值修改人，修改id，修改时间
        Values.push(`Created_By='${req.auth.name}'`);
        Values.push(`Created_ID=${req.auth.id}`);
        Values.push(`Created_Data='${dayjs().format("YYYY-MM-DD")}'`);
        // 修改审核数据
        var sql2 = `update god_generation set ${Values.join(",")} where id = ${
          req.body.id
        }`;
        db.query(sql2, function (err, rows) {
          if (err) {
            res.send({
              msg: "修改失败",
              success: false,
            });
          } else {
            res.send({
              msg: "修改成功",
              success: true,
            });
          }
        });
      }
    });
  }
});
// 修改wiki图片——————————————————————————————————————————————————————————————————————————————————————————————————————
router.post("/ImageConfiguration", (req, res) => {
  console.log("修改wiki图片");
  // 获取请求头传回来的更新列与更新id
  let state = req.get("state");
  // 判断传回来的更新状态和更新审核单号是否有空
  if (!state) {
    res.send({
      msg: "上传失败！",
      success: false,
    });
  } else {
    // 修改文件名的绝对路径
    let url = null;
    // 保存文件的路径
    let URL = null;
    // 文件名
    let urlName = "";
    // 判断到底是什么图片提交赋值对应的保存修改的路径
    if (state === "0") {
      url = path.join(`${key.URL}Home/`);
      URL = "/../public/images/Home";
      urlName = "top";
    }
    // 进行保存图片
    let form = formidable({
      multiples: true,
      uploadDir: __dirname + URL, //指定上传到当前项目的那个文件下
      keepExtensions: true, //是否保留后缀名
    });
    //上传完成后的操作
    form.parse(req, (err, fields, files) => {
      if (err) {
        next(err);
        return;
      }
      // 修改文件名，原文件绝对路径，修改后文件绝对路径
      fs.rename(url + files.file.newFilename, url + urlName + `.png`, (err) => {
        console.log(err);
      });
    });
    res.send({
      success: true,
    });
  }
});
// 称号一览修改————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
router.post("/TitleEdit", (req, res) => {
  console.log("称号一览修改");
  if (req.body.id) {
    // 查找审核库是否有对应数据
    const sql = `SELECT * from title_edit WHERE id=${req.body.id}`;
    db.query(sql, (err, result) => {
      if (err) {
        return;
      }
      if (result.length === 0) {
        res.send({
          msg: "找不到对应数据",
          success: false,
        });
      } else {
        // 获取前端key字段和Value提取出来
        let Key = Object.keys(req.body.row);
        let Value = Object.values(req.body.row);
        // 数据库赋值数据字段的字段
        let Values = [];
        // 循环前端传回来的数据valise
        Key.some((item, index) => {
          // 如果为空值则赋值null
          if (Value[index] === null || Value[index] === "") {
            Values.push(`${item}=null`);
          } else if (typeof Value[index] === "object") {
            // 如果是数组则转化完后赋值
            Values.push(`${item}='${JSON.stringify(Value[index])}'`);
          } else {
            // 其余照常赋值
            Values.push(`${item}='${Value[index]}'`);
          }
        });
        // 赋值修改人，修改id，修改时间
        Values.push(`Created_By='${req.auth.name}'`);
        Values.push(`Created_ID=${req.auth.id}`);
        Values.push(`Created_Data='${dayjs().format("YYYY-MM-DD")}'`);
        // 修改审核数据
        var sql2 = `update title_edit set ${Values.join(",")} where id = ${
          req.body.id
        }`;
        db.query(sql2, function (err, rows) {
          if (err) {
            res.send({
              msg: "修改失败",
              success: false,
            });
          } else {
            res.send({
              msg: "修改成功",
              success: true,
            });
          }
        });
      }
    });
  }
});

// 封灭战修改————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
router.post("/WarOfAnnihilation", (req, res) => {
  console.log("封灭战修改");
  if (req.body.state === "审核库") {
    // 查找审核库是否有对应数据
    const sql = `SELECT * from war_of_annihilation WHERE id=${req.body.id} AND Agree=0`;
    db.query(sql, (err, result) => {
      if (err) {
        return;
      }
      if (result.length === 0) {
        res.send({
          msg: "找不到对应数据",
          success: false,
        });
      } else {
        //  判断该数据是否还未通过审批
        if (result[0].Agree === 0) {
          //将时间变化为可赋值字段
          req.body.row.Created_Data = req.body.row.Created_Data.split("T")[0];
          // 获取前端key字段和Value提取出来
          let Key = Object.keys(req.body.row);
          let Value = Object.values(req.body.row);
          // 数据库赋值数据字段的字段
          let Values = [];
          // 循环前端传回来的数据valise
          Key.some((item, index) => {
            // 如果为空值则赋值null
            if (Value[index] === null || Value[index] === "") {
              Values.push(`${item}=null`);
            } else if (typeof Value[index] === "object") {
              // 如果是数组则转化完后赋值
              Values.push(`${item}='${JSON.stringify(Value[index])}'`);
            } else {
              // 其余照常赋值
              Values.push(`${item}='${Value[index]}'`);
            }
          });

          // 修改审核数据
          var sql2 = `update war_of_annihilation set ${Values.join(
            ","
          )} where id = ${req.body.id}`;
          db.query(sql2, function (err, rows) {
            if (err) {
              res.send({
                msg: "修改失败",
                success: false,
              });
            } else {
              res.send({
                msg: "修改成功",
                success: true,
              });
            }
          });
        } else {
          res.send({
            msg: "该审核数据已经被审核",
            success: false,
          });
        }
      }
    });
  } else if (req.body.state === "正式库") {
    // 查找审核库是否有对应数据
    const sql = `SELECT * from war_of_annihilation WHERE id=${req.body.id} AND Agree=1`;
    db.query(sql, (err, result) => {
      if (err) {
        return;
      }
      if (result.length === 0) {
        res.send({
          msg: "找不到对应数据",
          success: false,
        });
      } else {
        //将时间变化为可赋值字段
        req.body.row.Created_Data = req.body.row.Created_Data.split("T")[0];
        // 将状态，是否同意、创建人、创建人id、创建时间重新赋值
        req.body.row.state = 1;
        req.body.row.Agree = 0;
        req.body.row.Created_By = req.auth.name;
        req.body.row.Created_ID = req.auth.id;
        req.body.row.Created_Data = `${new Date().getFullYear()}-${
          new Date().getMonth() + 1 <= 10
            ? `0${new Date().getMonth() + 1}`
            : new Date().getMonth() + 1
        }-${
          new Date().getDate() <= 10
            ? `0${new Date().getDate()}`
            : new Date().getDate()
        }`;
        // 获取前端key字段和Value提取出来
        let Key = Object.keys(req.body.row);
        let Value = Object.values(req.body.row);
        // 数据库赋值数据字段的字段
        let Values = [];
        // 循环前端传回来的数据valise
        Key.some((item, index) => {
          // 如果为空值则赋值null
          if (Value[index] === null || Value[index] === "") {
            Values.push(`${item}=null`);
          } else if (typeof Value[index] === "object") {
            // 如果是数组则转化完后赋值
            Values.push(`${item}='${JSON.stringify(Value[index])}'`);
          } else {
            // 其余照常赋值
            Values.push(`${item}='${Value[index]}'`);
          }
        });

        // 修改审核数据
        var sql2 = `update war_of_annihilation set ${Values.join(
          ","
        )} where id = ${req.body.id}`;
        db.query(sql2, function (err, rows) {
          if (err) {
            res.send({
              msg: "修改失败",
              success: false,
            });
          } else {
            res.send({
              msg: "修改成功",
              success: true,
            });
          }
        });
      }
    });
  } else {
    res.send({
      msg: "修改失败",
      success: false,
    });
  }
});
// 视频攻略合集修改————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
router.post("/capture", (req, res) => {
  console.log("视频攻略合集修改");
  if (req.body.id) {
    // 查找审核库是否有对应数据
    const sql = `SELECT * from capture WHERE id=${req.body.id}`;
    db.query(sql, (err, result) => {
      if (err) {
        return;
      }
      if (result.length === 0) {
        res.send({
          msg: "找不到对应数据",
          success: false,
        });
      } else {
        // 获取前端key字段和Value提取出来
        let Key = Object.keys(req.body.row);
        let Value = Object.values(req.body.row);
        // 数据库赋值数据字段的字段
        let Values = [];
        // 循环前端传回来的数据valise
        Key.some((item, index) => {
          if (
            item === "Created_By2" ||
            item === "Created_ID2" ||
            item === "Created_Data2"
          ) {
            if (item === "Created_By2") {
              Values.push(`${item}='${req.auth.name}'`);
            } else if (item === "Created_ID2") {
              Values.push(`${item}='${req.auth.id}'`);
            } else if (item === "Created_Data2") {
              Values.push(`${item}='${dayjs().format("YYYY-MM-DD")}'`);
            }
          } else {
            // 如果为空值则赋值null
            if (Value[index] === null || Value[index] === "") {
              Values.push(`${item}=null`);
            } else if (typeof Value[index] === "object") {
              // 如果是数组则转化完后赋值
              Values.push(`${item}='${JSON.stringify(Value[index])}'`);
            } else {
              // 其余照常赋值
              Values.push(`${item}='${Value[index]}'`);
            }
          }
        });
        // 修改审核数据
        var sql2 = `update capture set ${Values.join(",")} where id = ${
          req.body.id
        }`;
        db.query(sql2, function (err, rows) {
          if (err) {
            res.send({
              msg: "修改失败",
              success: false,
            });
          } else {
            res.send({
              msg: "修改成功",
              success: true,
            });
          }
        });
      }
    });
  }
});
module.exports = router;
